<template>
<view>
<block v-if="now_status==1">
	<view class="qp_img_box">
		<image mode="aspectFill" :src="webUrl + 'addons/monai_market/static/wimgs/jsz_img.png'"></image>
	</view>
</block>
<block v-if="now_status==0">
	<form @reset="formReset" @submit="release" reportSubmit="true">
		<view class="modle" style="border-bottom:0">
			图片
		</view>
		<view v-for="(item, index) in carsimgs" :key="index" class="shangchuan_view" v-if="item.img!=''">
			<image @tap="upthiscarsimgs" class="shangchuan_Img_close" :data-index="index" src="/static/pages/image/tupiangb_btn.png"></image>
			<image @tap="showimgs" class="shangchuan_Img" :data-index="index" :src="item.img"></image>
		</view>
		<view class="shangchuan_view" v-if="carsimgs.length<1&&tid==''">
			<image @tap="upcarsimgs" class="shangchuan_Img" src="/static/pages/image/tianjiantp_btn.png"></image>
		</view>
		<view class="shangchuan_view" v-if="carsimgs.length<1&&tid!=''">
			<image @tap="upcarsimgs" class="shangchuan_Img" src="/static/pages/image/tianjiantp_btn.png"></image>
		</view>
		<view class="modle">
			标题
			<text class="must_red">*</text>
			<input class="modle_right" @input="inpTit" name="synopsis" placeholder="请输入标题" placeholderStyle="color: #9295A8;" type="text" :value="tit">
		</view>

		<view class="modle">
			姓名
			<text class="must_red">*</text>
			<input class="modle_right" @input="inpName" name="carname" placeholder="请输入姓名" placeholderStyle="color: #9295A8;" type="text" :value="userName">
		</view>
		<view class="modle">
			<input class="modle_right btm_input" @input="inpTel" name="phone" placeholder="请输入联系电话" placeholderStyle="color: #9295A8;" type="number" :value="userphone">
			联系电话
			<text class="must_red">*</text>
			<button @getphonenumber="getPhoneNumber" class="modle_btn" open-type="getPhoneNumber">电话授权</button>
		</view>
		<view class="modle">
			地址
			<text class="must_red">*</text>
			<!-- <input class="modle_right" bindinput="inpAddr" name="carname" placeholder="请输入地址" placeholderStyle="color: #9295A8;" type="text" value="{{userAddr}}"></input> -->
			<view class="modle_right">
				<region-picker @change="bindRegionChange" class="serch_border_area_border" :customItem="customItem" :value="region">
					<view class="picker">
						{{region[0]||(userAddr!=null&&userAddr!='')?userAddr:'请选择'}}
					</view>
				</region-picker>
			</view>
		</view>
		<view class="modle line_bott">
			内容
			<text class="must_red">*</text>
			<!-- <input class="modle_right" name="carname" placeholder="请输入内容" placeholderStyle="color: #9295A8;" type="text" value="{{defult_value}}"></input> -->

		</view>
		<view class="texta_box">
			<textarea placeholder="请输入内容" @input="inpCont" maxlength="-1" :value="content"></textarea>
		</view>

		<!-- <view class="modle">
		<input class="modle_right btm_input" disabled="true" name="address" placeholder="" placeholderStyle="color: #9295A8;" type="text" value="{{mapname&&mapname.length>2?mapname:''}}"></input>
		车辆地址
		<text class="must_red">*</text>
		<button bindtap="tomap" class="modle_btn">获取位置</button>
	</view> -->
		<block v-if="tid==''">
			<button class="bufa_btn" style="background: #333;" v-if="status=='1'">已经禁止{{st_word}}信息</button>
			<button class="bufa_btn" @tap="click_send" v-else>{{st_word}}</button>
		</block>
		<block v-if="tid!=''">
			<button class="bufa_btn" style="background: #333;" v-if="status=='1'">已经禁止{{st_word}}信息</button>
			<button class="bufa_btn" @tap="click_update" v-else>修改</button>
		</block>
		<view class="jinggao">{{st_word}}不真实信息，一经核查将做封号处理。</view>
	</form>
	<!--<import src="/pages/alert/index.wxml"></import>-->
	<block data-type="template" data-is="alert" data-attr="isShow:isShow">
    <view class="wx_dialog_container" :hidden="!isShow">
        <view class="wx-alert-mask"></view>
        <view class="wx-alert-dialog">
            <view class="wx-alert-dialog-title">尊敬的用户</view>
            <view class="wx-alert-dialog-content">尚未获取到您的用户信息，较多功能无法体验，请允许获取用户信息</view>
            <view class="wx-alert-dialog-footer">
                <button @tap="hideDialog" class="wx-alert-dialog-btn">拒绝</button>
                <button @getuserinfo="updateUserInfo" class="wx-alert-dialog-btn" open-type="getUserInfo">允许</button>
            </view>
        </view>
    </view>
</block>
</block>
</view>
</template>

<script>
var app = getApp(),
    upurl = app.globalData.util.url("entry/wxapp/Api", {
  m: "monai_market",
  uid: wx.getStorageSync("uid"),
  r: "Upimg"
});

export default {
  data() {
    return {
      carsimgs: [],
      userphone: '',
      tit: '',
      userName: '',
      content: '',
      ty: '',
      tid: '',
      id: '',
      userAddr: '',
      region: ["", "", ""],
      st_word: '',
      webUrl: '',
      now_status: 1,
      customItem: "",
      status: ""
    };
  },

  components: {},
  props: {},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options.tid);
    var that = this;
    that.check_st();
    that.check_status();
    app.globalData.imgUrl();
    setTimeout(() => {
      that.setData({
        webUrl: wx.getStorageSync('webUrl')
      });
    }, 500);

    if (options.tid) {
      that.setData({
        tid: options.tid,
        id: options.id
      });
      that.getDet(options.id);
    } else {
      that.setData({
        ty: options.ty
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  methods: {
    check_status: function () {
      var that = this;
      app.globalData.util.request({
        url: "entry/wxapp/Api",
        data: {
          m: "monai_market",
          r: "home.index.examine"
        },
        success: function (res) {
          console.log('状态', res.data);
          that.setData({
            now_status: res.data.data
          });
        },
        fail: function (res) {}
      });
    },
    // 切换地区
    bindRegionChange: function (a) {
      var that = this;
      var t = "不限" == a.detail.value[0] ? "" : a.detail.value[0],
          e = "不限" == a.detail.value[1] ? "" : a.detail.value[1],
          i = "不限" == a.detail.value[2] ? "" : a.detail.value[2],
          addr = t + '-' + e + '-' + i;
      that.setData({
        region: [t, e, i],
        userAddr: addr
      });
      wx.setStorageSync('addr', that.region);
    },
    check_st: function () {
      var that = this;
      app.globalData.util.request({
        url: "entry/wxapp/Api",
        data: {
          r: "home.index.change_word",
          m: "monai_market"
        },
        cachetime: "0",
        success: function (res) {
          console.log('abc:', res.data);
          var json = res.data.data;
          wx.setNavigationBarTitle({
            title: res.data.data
          });
          that.setData({
            st_word: res.data.data
          });
        }
      });
    },
    //获取详情
    getDet: function (e) {
      var that = this;
      app.globalData.util.request({
        url: "entry/wxapp/Api",
        data: {
          r: "home.index.user_workrentdetial",
          m: "monai_market",
          uid: app.globalData.getuid(),
          id: e
        },
        cachetime: "0",
        success: function (res) {
          console.log('详情：', res.data);
          var json = res.data.data;
          that.setData({
            carsimgs: json.img_str,
            userphone: json.tel,
            tit: json.title,
            userName: json.name,
            content: json.content,
            userAddr: json.address
          });
        }
      });
    },
    //获取标题
    inpTit: function (e) {
      var that = this;
      var t = e.detail.value;
      that.setData({
        tit: t
      });
    },
    //获取地址信息
    inpAddr: function (e) {
      var that = this;
      var t = e.detail.value;
      that.setData({
        userAddr: t
      });
    },
    //获取姓名
    inpName: function (e) {
      var that = this;
      var t = e.detail.value;
      that.setData({
        userName: t
      });
    },
    //获取电话
    inpTel: function (e) {
      var that = this;
      var t = e.detail.value;
      that.setData({
        userphone: t
      });
    },
    //获取内容
    inpCont: function (e) {
      var that = this;
      var t = e.detail.value;
      that.setData({
        content: t
      });
    },
    //上传图片
    upcarsimgs: function (a) {
      var e = this;
      9 <= e.carsimgs.length ? app.globalData.util.message({
        title: "您添加的图片已经很多了",
        type: "error"
      }) : app.globalData.upimgs({
        scannums: 9,
        filename: "cars",
        success: function (a) {
          for (var t in a) e.addinimgs(a[t]);
        }
      });
    },
    addinimgs: function (a) {
      console.log(a);
      var t = this.carsimgs,
          e = {
        img: a.all,
        imgshort: a.short
      };
      console.log(t);
      t.push(e), this.setData({
        carsimgs: t
      });
      console.log(this.carsimgs);
    },
    upthiscarsimgs: function (a) {
      var t = a.currentTarget.dataset.index,
          e = this.carsimgs;
      app.globalData.util.request({
        url: "entry/wxapp/Api",
        data: {
          m: "monai_market",
          uid: app.globalData.getuid(),
          imgurl: e[t].img,
          r: "Upimg.delImg"
        }
      }), e.splice(t, 1), this.setData({
        carsimgs: e
      });
      console.log(this.carsimgs);
    },
    //获取电话号码
    getPhoneNumber: function (a) {
      console.log(a);
      var t = this;
      app.globalData.util.request({
        url: "entry/wxapp/Api",
        data: {
          r: "sale.index.userphone",
          m: "monai_market",
          iv: a.detail.iv,
          encryptedData: a.detail.encryptedData
        },
        cachetime: "0",
        success: function (a) {
          t.setData({
            userphone: a.data.data
          });
        }
      });
    },
    //点击修改按钮
    click_update: function () {
      var that = this;
      var where = 3;

      if (that.tid == 2) {
        where = 4;
      }

      if (that.carsimgs.length != 0) {
        if (that.tit != '' && that.tit != null) {
          if (that.userName != '' && that.userName != null) {
            if (that.userphone != '' && that.userphone != null) {
              if (that.userAddr != '' && that.userAddr != null) {
                if (that.content != '' && that.content != null) {
                  app.globalData.util.request({
                    url: "entry/wxapp/Api",
                    data: {
                      r: "home.index.useredit_workrent",
                      m: "monai_market",
                      uid: app.globalData.getuid(),
                      id: that.id,
                      title: that.tit,
                      name: that.userName,
                      address: that.userAddr,
                      tel: that.userphone,
                      content: that.content,
                      carimgs: that.carsimgs[0].imgshort
                    },
                    cachetime: "0",
                    success: function (a) {
                      console.log('修改成功：', a.data);
                      wx.showToast({
                        title: '修改成功',
                        success: function () {
                          wx.reLaunch({
                            url: '../mine/index'
                          });
                        }
                      });
                    }
                  });
                } else {
                  wx.showToast({
                    title: '请填写内容',
                    icon: 'none'
                  });
                }
              } else {
                wx.showToast({
                  title: '请填写地址',
                  icon: 'none'
                });
              }
            } else {
              wx.showToast({
                title: '请填写联系电话',
                icon: 'none'
              });
            }
          } else {
            wx.showToast({
              title: '请填写姓名',
              icon: 'none'
            });
          }
        } else {
          wx.showToast({
            title: '请填写标题',
            icon: 'none'
          });
        }
      } else {
        wx.showToast({
          title: '请上传图片',
          icon: 'none'
        });
      }
    },
    //点击发布按钮
    click_send: function () {
      var that = this;
      var where = 1;

      if (that.ty == 2) {
        where = 2;
      }

      if (that.carsimgs.length != 0) {
        if (that.tit != '' && that.tit != null) {
          if (that.userName != '' && that.userName != null) {
            if (that.userphone != '' && that.userphone != null) {
              if (that.userAddr != '' && that.userAddr != null) {
                if (that.content != '' && that.content != null) {
                  app.globalData.util.request({
                    url: "entry/wxapp/Api",
                    data: {
                      r: "home.index.fbworkrent",
                      m: "monai_market",
                      uid: app.globalData.getuid(),
                      type: that.ty,
                      title: that.tit,
                      name: that.userName,
                      tel: that.userphone,
                      address: that.userAddr,
                      content: that.content,
                      carimgs: that.carsimgs[0].imgshort
                    },
                    cachetime: "0",
                    success: function (a) {
                      console.log('发布成功：', a.data); // wx.showToast({
                      //   title: '发布成功',
                      //   success:function(){
                      //     wx.navigateTo({
                      //       url: '../joinUs/joinUs?wh='+where,
                      //     })
                      //   }
                      // })

                      wx.showModal({
                        content: '发布成功',
                        confirmText: '好的',
                        cancelText: '继续添加',
                        success: function (res) {
                          if (res.confirm) {
                            console.log('好的');
                            wx.switchTab({
                              url: '../index/index'
                            });
                          } else {
                            console.log('继续添加');
                            wx.navigateTo({
                              url: '/pagesA/sJobLease/sJobLease?ty=' + where
                            });
                          }
                        }
                      });
                    }
                  });
                } else {
                  wx.showToast({
                    title: '请填写内容',
                    icon: 'none'
                  });
                }
              } else {
                wx.showToast({
                  title: '请填写地址',
                  icon: 'none'
                });
              }
            } else {
              wx.showToast({
                title: '请填写联系电话',
                icon: 'none'
              });
            }
          } else {
            wx.showToast({
              title: '请填写姓名',
              icon: 'none'
            });
          }
        } else {
          wx.showToast({
            title: '请填写标题',
            icon: 'none'
          });
        }
      } else {
        wx.showToast({
          title: '请上传图片',
          icon: 'none'
        });
      }
    },

    formReset() {
      console.log("占位：函数 formReset 未声明");
    },

    release() {
      console.log("占位：函数 release 未声明");
    },

    showimgs() {
      console.log("占位：函数 showimgs 未声明");
    }

  }
};
</script>
<style>
@import "./sJobLease.css";
</style>